<?php
/**
 * @author fld 2021-06-21
 * @desc 后台操作日志业务类
 */
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;
use App\Exports\ExcelExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\ExcelImport;

class AdminActionlogService
{
    /**
     * @author fld 2021-06-21
     * @desc 添加后台操作日志信息
     * @param $data
     * @return int
     */
    public function addAdminActionLog($data){
        $admin = session('admin');
        $insert_data = [];
        $insert_data['log_uid'] = $admin['id'];
        $insert_data['log_name'] = $admin['admin_name'];
        $insert_data['log_true_name'] = $admin['true_name'];
        $insert_data['log_module'] = $data['module_name'];
        $insert_data['log_type'] = $data['type'];
        $insert_data['log_remark'] = $data['log_remark'];
        $insert_data['log_time'] = time();
        $insert_data['log_ip'] = request()->getClientIp();
        $id = DB::table('admin_actionlog')->insertGetId($insert_data);
        return $id;
    }

    /**
     * @author fld 2021-06-18
     * @desc 登录日志分页列表
     * @return array
     */
    public function getLogListByPage(){
        $params = request()->all();
        $condition = $this->getSearchCondition();
        $list = DB::table('admin_actionlog')->where($condition)->orderBy('id','desc')->paginate(10);
        $pager = $list->links();

        $module_list = DB::table('admin_actionlog')->select('log_module')->distinct()->get();
        $module_list = objectToArray($module_list);
        return ['list' => $list, 'pager' => $pager, 'params'=>$params, 'module_list'=>$module_list];
    }

    /**
     * @author fld 2021-06-17
     * @desc 查询条件
     * @return array
     */
    public function getSearchCondition(){
        $condition = [];
        $params = request()->all();
        if(!empty($params['name'])){
            $condition[] = ['log_name','like','%'.$params['name'].'%'];
        }
        if(!empty($params['true_name'])){
            $condition[] = ['log_true_name','like','%'.$params['true_name'].'%'];
        }
        if(!empty($params['module'])){
            $condition[] = ['log_module','=', $params['module']];
        }
        if(!empty($params['type'])){
            $condition[] = ['log_type','=', $params['type']];
        }
        if(!empty($params['remark'])){
            $condition[] = ['log_remark','like','%'.$params['remark'].'%'];
        }
        if(!empty($params['begin_time'])){
            $condition[] = ['log_time','>=', strtotime($params['begin_time'])];
        }
        if(!empty($params['end_time'])){
            $condition[] = ['log_time','<=', strtotime($params['end_time'])];
        }
        return $condition;
    }
    /**
     * @author fld 2021-08-27
     * @desc 导出数据
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function export(){
        //设置表头
        $row = [[
            "id"=>'ID',
            "log_name"=>'账号',
            "log_true_name"=>'姓名',
            "log_module"=>'操作模块',
            "log_type"=>'操作状态',
            "log_remark"=>'操作内容',
            "log_time"=>'操作时间',
            "log_ip"=>'操作ip',
        ]];
        $params = request()->all();
        $condition = $this->getSearchCondition();
        $field = ['id','log_name','log_true_name','log_module','log_type','log_remark','log_time','log_ip'];
        $list = DB::table('admin_actionlog')->where($condition)->orderBy('id','desc')->get($field);
        $list = objectToArray($list);
        foreach ($list as $k=>&$v){
            $v['log_time'] = date('Y-m-d H:i:s',$v['log_time']);
        }
        $data = $list;//导出的数据
        $headings = $row;//导出的标题头部
        $excel = new ExcelExport($data, $headings, 'Sheet1');
        /* $excel->setColumnWidth(['B' => 40, 'C' => 40]);
        $excel->setRowHeight([1 => 40, 2 => 50]);
        $excel->setFont(['A1:Z1265' => '宋体']);
        $excel->setFontSize(['A1:I1' => 14,'A2:Z1265' => 10]);
        $excel->setBold(['A1:Z2' => true]);
        $excel->setBackground(['A1:A1' => '808080','C1:C1' => '708080']);
        $excel->setMergeCells(['A1:I1']);
        $excel->setBorders(['A2:D5' => '#000000']);  */
        
        return Excel::download($excel, "操作数据_".date('YmdHis').".xlsx");
    }
    
    /**
     * @author fld 2021-09-08
     * @desc 导入数据
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function import($request){
        $file_name = "file_name";
        $file = $request->file($file_name);
        if($request->hasFile($file_name) && $file->isValid()){
            $realpath = $file->getRealPath();
            $del_title = 1;
            $excel = new ExcelImport($del_title);
            Excel::import($excel, $realpath);
            $data = $excel->data;
            print_r($data);
            return ['code'=>0,'message'=>'导入数据成功'];
        }else{
            return ['code'=>101,'message'=>'上传文件不存在'];
        }
        
    }
}